package com.leo.springbootmultithreadbasic.forkjoin;

import org.springframework.util.Assert;

import java.util.concurrent.*;

/**
 * @author Leo
 * @version V1.0
 */
public class MainDemo {
    public static void main(String[] args) {
        test();
    }

    /**
     * 使用ForkJoinPool调度AccumulateTask()
     */
    public static void test() {
        ForkJoinPool forkJoinPool = new ForkJoinPool();

        // 创建一个累加任务，计算由1加到100
        AccumulateTask accumulateTask = new AccumulateTask(1, 100);
        Future<Integer> future = forkJoinPool.submit(accumulateTask);
        try {
            // 从 future 中 get 结果
            int sum = future.get(1, TimeUnit.SECONDS);
            System.out.println("最终计算结果 = " + sum);
            Assert.isTrue(sum ==5050, "最终计算结果正确");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
